**************************************************************************************
* Table 1
**************************************************************************************
use  "Data_temp/turnout.dta", clear

estimates clear

*****************************************
* t0-t3
forvalues x = 0/3 {
	
	* Generate election dummies & drop first one
	qui tab election if voted<. & inrange(age`x',-24,24) & age`x'!=0, gen(dummy)
	drop dummy1
	
	rdrobust voted age`x' if age`x'!=0 ,  p(1)  h(24) covs(dummy*)
		est sto m`x'
		estadd scalar  eff_obs=e(N_b_l)+e(N_b_r)

		drop dummy*
	}

*****************************************
* Pooled estimates for t1-t3
	
* Duplicates for observations that would occur multiply in pooled observations
gen m1=1 if inrange(age1,-24,24) & inrange(age2,-24,24)		
tab m1
expand 2 if m1==1, gen(dupl12)
replace age2=. if dupl12==1 // ignore those from duplicate observations
replace age1=. if dupl12==0 & inrange(age2,-24,24) //
count if inrange(age1,-24,24) & inrange(age2,-24,24)

gen m2=1 if inrange(age2,-24,24) & inrange(age3,-24,24)		
expand 2 if m2==1, gen(dupl23)
replace age2=. if dupl23==1
replace age3=. if dupl23==0 & inrange(age2,-24,24) 
count if inrange(age2,-24,24) & inrange(age3,-24,24)

* Generate relative age for each election
gen rel_age=.
forvalues x = 1/3 {
		replace rel_age=age`x' if inrange(age`x',-24,24) & rel_age==.
		}

cap drop dummy*
tab election if voted<. & inrange(rel_age,-24,24) & rel_age!=0, gen(dummy)
drop dummy1	
	
rdrobust voted rel_age if rel_age!=0, covs(dummy*) p(1)  h(24)
	est sto m4
	estadd scalar  eff_obs=e(N_b_l)+e(N_b_r)

		
estout m* using "Tables/tb1.tex", ///
	style(tex) cells (b(star fmt(%9.3f)) se(par fmt(%9.3f)))  ///
	starlevels( * 0.1 ** 0.05 *** 0.01)  varlabels(RD_Estimate "\hline \\ \\ RD estimate")	  ///
	stats(eff_obs, fmt(%9.0gc) labels("\hline N"))    mlabels(none	) ///
	collabels(none) eql(none) notype label replace	